package com.sun.dream.fish.config;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.IFill;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.zaxxer.hikari.util.FastList;

/**
 * 自动生成代码器
 */
public class GeneratorCodeConfig {
    /**
     * 数据源配置
     */
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig.Builder("jdbc:mysql://127.0.0.1:3306/dream_salted_fish?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true",
            "root", "root");
    public static void main(String[] args) {
        String[] tables = {"t_user",
                "t_nav_meta",
                "t_role"};
        tables = args;
        Column column = new Column("create_time", FieldFill.INSERT);
        // 代码生成器
        String[] finalTables = tables;
        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                // 全局配置
                .globalConfig(builder -> builder.author("ljl")  //作者
                                .outputDir(System.getProperty("user.dir") + "/src/main/java") //输出目录
//                        .fileOverride()) //文件覆盖
                                .disableOpenDir()  //是否打开资源管理器
                )
                // 包配置
                .packageConfig(builder -> builder
                        .parent("com.sun.dream.fish")
                        .service("service")
                        .serviceImpl("service.impl")
                        .mapper("mapper")
                        .xml("mapper.xml")
                        .entity("entity")
                )
                // 策略配置
                .strategyConfig(builder -> builder.addInclude(finalTables)
                        .entityBuilder().formatFileName("%sEntity").enableLombok().enableTableFieldAnnotation().columnNaming(NamingStrategy.underline_to_camel).idType(IdType.AUTO).enableChainModel().addTableFills(column).logicDeleteColumnName("deleted").enableFileOverride()
                        .mapperBuilder().formatMapperFileName("%sMapper").formatXmlFileName("%sMapper")
                        .serviceBuilder().formatServiceFileName("%sService").formatServiceImplFileName("%sServiceImpl")
                        .controllerBuilder().enableRestStyle().enableHyphenStyle()
                ).execute();
    }
}
